package com.macro.mall.portal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.*;
import com.macro.mall.model.*;
import com.macro.mall.portal.dao.PortalSkuStockDao;
import com.macro.mall.portal.domain.CombinedPmsProduct;
import com.macro.mall.portal.dto.PlantingProductDto;
import com.macro.mall.portal.service.PmsPortalProductService;
import com.macro.mall.portal.service.UmsMemberService;
import com.macro.mall.portal.service.UmsPlantingService;
import local.com.quyum.aliyun.aliyunoss.service.VodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UmsPlantingServiceImpl implements UmsPlantingService {
    @Autowired
    private UmsPlantingTechnologyTypeMapper plantingTypeMapper;
    @Autowired
    private UmsPlantingTechnologyMapper plantingMapper;
    @Autowired
    private UmsMemberService memberService;
    @Autowired
    private PmsPlantingTechnologyProductMapper planPlanductMapper;
    @Autowired
    private PmsProductMapper productMapper;
    @Autowired
    private PmsPortalProductService productService;
    @Autowired
    private PortalSkuStockDao portalSkuStockDao;
    @Autowired
    private PmsSkuStockMapper skuStockMapper;
    @Autowired
    private PmsMemberPriceMapper priceMapper;
    @Autowired
    private VodService vodService;

    @Override
    public List<UmsPlantingTechnologyType> getPlantingType(Long storeId) {
//        UmsMember currentMember = memberService.getCurrentMember();
        UmsPlantingTechnologyTypeExample example = new UmsPlantingTechnologyTypeExample();
        example.createCriteria().andStoreIdEqualTo(storeId);
        return plantingTypeMapper.selectByExample(example);
    }

    @Override
    public List<UmsPlantingTechnology> getPlanting(Long storeId, Long typeId, String keyWord, Integer pageSize, Integer pageNum) {
//        UmsMember member = memberService.getCurrentMember();
        UmsPlantingTechnologyExample example = new UmsPlantingTechnologyExample();
        UmsPlantingTechnologyExample.Criteria criteria = example.createCriteria();
        if (typeId != null) {
            criteria.andTypeIdEqualTo(typeId);
        }
        if (StrUtil.isNotEmpty(keyWord)) {
            criteria.andTitleLike("%" + keyWord + "%");
        }
        criteria.andStoreIdEqualTo(storeId);
        List<UmsPlantingTechnology> result = new ArrayList<>();
        List<UmsPlantingTechnology> list = plantingMapper.selectByExample(example);
        PageHelper.clearPage();
        if (list != null && list.size() > 0) {
            return list;
        } else {
            return result;
        }
    }

    @Override
    public PlantingProductDto getPlantingById(Long id) {
        PlantingProductDto result = new PlantingProductDto();
        List<CombinedPmsProduct> list = new ArrayList<>();
        UmsPlantingTechnology plan = plantingMapper.selectByPrimaryKey(id);
        int pageView = 0;
        if(plan.getPageView()!=null){
             pageView = plan.getPageView() + 1;
        }
        plan.setPageView(pageView);
        plantingMapper.updateByPrimaryKeySelective(plan);
        PmsPlantingTechnologyProductExample example = new PmsPlantingTechnologyProductExample();
        example.createCriteria().andPlantingIdEqualTo(plan.getId());
        List<PmsPlantingTechnologyProduct> pmsPlantingTechnologyProducts = planPlanductMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(pmsPlantingTechnologyProducts)) {
            for (PmsPlantingTechnologyProduct pmsPlantingTechnologyProduct : pmsPlantingTechnologyProducts) {
                CombinedPmsProduct combinedProduct = new CombinedPmsProduct();
                PmsProduct product = productMapper.selectByPrimaryKey(pmsPlantingTechnologyProduct.getProductId());
                //普通商品
                if (product.getType().equals(0)) {
                    BeanUtil.copyProperties(product, combinedProduct);
                    PmsSkuStockExample stockExample = new PmsSkuStockExample();
                    stockExample.createCriteria().andProductIdEqualTo(product.getId());
                    List<PmsSkuStock> pmsSkuStocks = skuStockMapper.selectByExample(stockExample);
                    if(!CollectionUtils.isEmpty(pmsSkuStocks)){
                        //该商品的其中一个规格
                        PmsSkuStock pmsSkuStock = pmsSkuStocks.get(0);
                        combinedProduct.setPmsSkuStock(pmsSkuStock);//父商品规格
                        //该规格会员价
                        combinedProduct.setMemberPrice(gerSkuMemberPrice(pmsSkuStock.getId()));
                    }
                    list.add(combinedProduct);
                }else{
                    //组合商品
                    //获得商品规格id
                    PmsSkuStockExample stockExample = new PmsSkuStockExample();
                    stockExample.createCriteria().andProductIdEqualTo(pmsPlantingTechnologyProduct.getProductId());
                    //父商品的规格
                    List<PmsSkuStock> pmsSkuStocks = skuStockMapper.selectByExample(stockExample);
                    if(!CollectionUtils.isEmpty(pmsSkuStocks)){
                        List<Long> collect = pmsSkuStocks.stream().map(PmsSkuStock::getId).collect(Collectors.toList());
                        List<CombinedPmsProduct> combinedPmsProductList = portalSkuStockDao.queryProductsByCombineParentIds(collect);
                        //组合商品
                        CombinedPmsProduct combinedPmsProduct = combinedPmsProductList.get(0);
                        combinedProduct = productService.savePrice(combinedPmsProduct);
                        combinedProduct.setMemberPrice(gerSkuMemberPrice(pmsSkuStocks.get(0).getId()));
                    }
                    list.add(combinedProduct);
                }
            }

        }
        BeanUtil.copyProperties(plan, result);
        if(org.apache.commons.lang3.StringUtils.isNotBlank(result.getVideoId())){
            result.setVideoUrl(this.vodService.getVideoUrl(result.getVideoId()));

        }
        result.setList(list);
        return result;
    }

    public BigDecimal gerSkuMemberPrice(Long skuId){
        PmsMemberPriceExample priceExample = new PmsMemberPriceExample();
        priceExample.createCriteria().andProductStockIdEqualTo(skuId);
        List<PmsMemberPrice> pmsMemberPrices = priceMapper.selectByExample(priceExample);
        if(!CollectionUtils.isEmpty(pmsMemberPrices)){
            BigDecimal memberPrice = pmsMemberPrices.get(0).getMemberPrice();
            return memberPrice;
        }else{
            return null;
        }
    }
}
